﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using DotNetNuke.Entities.Modules;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke.UI.Widgets
{
    public class Authors : PortalModuleBase
    {
        public List<BlogAuthor> BlogAuthors { get { return _authors; } }

        private List<BlogAuthor> _authors;
        private XDocument _settings;
        private Dictionary<string, string> _widgetSettings;
        private IEnumerable<PostInfo> _posts;
        private string _authorsSortedBy;
        private string _authorsSortOrder;
        private bool _authorsShowPostCount;

        public class BlogAuthor
        {
            public DotNetNuke.Entities.Users.UserInfo Author;
            public int Count = 0;
        }

        public Authors()
        {
        }

        public Authors(XDocument settings, Dictionary<string, string> widgetSettings, IEnumerable<PostInfo> posts)
        {
            _settings = settings;
            _widgetSettings = widgetSettings;
            _posts = posts;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            CheckWidget();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            RenderAuthors();
        }

        private void CheckWidget()
        {
            if (!_widgetSettings["Widget"].Equals("Authors"))
                throw new System.Exception("Control doesn't implement DesertedRoadStudios.BlogNetNuke.UI.Widgets." + _widgetSettings["Widget"]);

            if (!_widgetSettings.ContainsKey("AuthorsSortedBy"))
                _authorsSortedBy = "Username";
            else
                _authorsSortedBy = _widgetSettings["AuthorsSortedBy"];

            if (!_widgetSettings.ContainsKey("AuthorsSortOrder"))
                _authorsSortOrder = "Descending";
            else
                _authorsSortOrder = _widgetSettings["AuthorsSortOrder"];

            if (!_widgetSettings.ContainsKey("AuthorsShowPostCount"))
                _authorsShowPostCount = false;
            else
                _authorsShowPostCount = Convert.ToBoolean(_widgetSettings["AuthorsShowPostCount"]);
        }

        private void RenderAuthors()
        {
            IEnumerable<int> authorIDs = _posts.Select(p => p.AuthorID).Distinct();
            List<BlogAuthor> tempAuthors = new List<BlogAuthor>();
            
            foreach (int authorID in authorIDs)
            {
                BlogAuthor tempAuthor = new BlogAuthor();
                tempAuthor.Author = DotNetNuke.Entities.Users.UserController.GetUserById(Convert.ToInt32(_settings.Element("params").Element("Blog").Element("BlogDetails").Element("PortalID").Value), authorID);

                if (_authorsShowPostCount)
                    tempAuthor.Count = _posts.Where(p => p.AuthorID == authorID).Count();
    
                tempAuthors.Add(tempAuthor);
            }

            if (_authorsSortedBy == "Username")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.Username).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.Username).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "DisplayName")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.DisplayName).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.DisplayName).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "FirstName")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.FirstName).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.FirstName).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "LastName")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.LastName).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.LastName).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "FirstName LastName")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.FirstName + " " + a.Author.LastName).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.FirstName + " " + a.Author.LastName).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "LastName, FirstName")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Author.LastName + ", " + a.Author.FirstName).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Author.LastName + ", " + a.Author.FirstName).ToList<BlogAuthor>();
            }
            else if (_authorsSortedBy == "Posts Count")
            {
               if (_authorsSortOrder == "Ascending")
                   _authors = tempAuthors.OrderBy(a => a.Count).ToList<BlogAuthor>();
               else 
                   _authors = tempAuthors.OrderByDescending(a => a.Count).ToList<BlogAuthor>();
            }
        }
    }
}